import { reqGetGoodsInfo,reqAddUpdateShopCart } from "@/api";
import {getUUID} from "@/utils/uuid_token"

const state = {
    goodInfo : {},
    uuid_token:getUUID()
};

const mutations = {
    GETGOODINFO(state,goodInfo) {
        state.goodInfo = goodInfo
    }
};

const actions = {
    // 获取产品信息的action
    async getGoodInfo({commit},skuid) {
        const result = await reqGetGoodsInfo(skuid);
        if(result.code == 200) {
            commit("GETGOODINFO",result.data)
        }
    },
    // 将产品添加到购物车中
    async addOrUpdateShopCart({commit},{skuId,skuNum}) {
        let result = await reqAddUpdateShopCart(skuId,skuNum);
        // 代表加入成功
        if(result.code == 200) {
            return "ok"
        } else {
            return Promise.reject(new Error("faile"))
        }
    }
};

// 简化数据而生
const getters = {
    // 至少让它是一个对象，不然当数据为空的时候是undefined，undefined.对象会报错
    categoryView() {
        return state.goodInfo.categoryView || {};
    },
    skuInfo() {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList() {
        return state.goodInfo.spuSaleAttrList || [];
    }
};

export default {
    state,
    actions,
    mutations,
    getters
}